<?php

namespace app\index\controller;

use app\common\controller\Home;
use app\common\model\Users;

class Reg extends Home {

    public function index($id) 
    {
        $this->assign('token', $id);
        return $this->fetch();
    }

    public function register()
    {
        if ($data = request()->post()) {
            if (strlen($data['mobile']) != 11) {
                return json(['status' => 'fail', 'info' => '手机号不合法']);
            }
            if (strlen($data['password']) < 6) {
                return json(['status' => 'fail', 'info' => '密码不能少于6位']);
            }
            $token = json_decode(base64_decrypt($data['token'], config('user_key')), true);
            if (isset($token['reg_auth']) && $token['reg_auth'] == config('reg_auth')) {
                $inviter_id = $token['user_id'];
            } else {
                $inviter_id = 0;
            }
            $model = new Users();
            $ip = request()->ip();
            $res = $model->reg($data['mobile'], $data['password'], $ip, $inviter_id);
            if ($res === true) {
                return json(['status' => 'success', 'info' => '注册成功']);
            } else {
                return json(['status' => 'fail', 'info' => $res]);
            }
        } else {
            return json(['status' => 'fail', 'info' => '错误请求']);
        }
    }
}
